package com.tg.paw.ums.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tg.paw.ums.entity.Permission;
import com.tg.paw.ums.mapper.PermissionMapper;
import com.tg.paw.ums.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author Rubble
 * @since 2020-10-22
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {



    @Override
    public List<Permission> getPermissionByParentId(Long parentId) {
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permission::getParentId,parentId);
        return this.list(queryWrapper);
    }

    @Override
    public void fetchChildrenTree(Permission permission) {
        List<Permission> children = getPermissionByParentId(permission.getPermissionId());
        if(CollUtil.isNotEmpty(children)){
            permission.setChildren(children);
            for (Permission child : children) {
                fetchChildrenTree(child);
            }
        }
    }

    @Override
    public List<Permission> getUserPermissions(Long adminId) {
        return getBaseMapper().selectUserPermissions(adminId);
    }
}
